package com.zhiche.lisa.bms.controller.psup;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.dao.model.psup.PsupFixedPrice;
import com.zhiche.lisa.bms.pojo.vo.common.CommonVO;
import com.zhiche.lisa.bms.pojo.vo.psup.PsupFixedPriceVO;
import com.zhiche.lisa.bms.service.psup.PsupFixedPriceService;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 人送供方一口价 前端控制器
 * </p>
 *
 * @author linbao
 * @since 2019-06-18
 */
@RestController
@RequestMapping("/psupFixedPrice")
@Api(description = "人送一口价管理", tags = "人送一口价管理")
@Slf4j
public class PsupFixedPriceController extends PsupSdBaseController {

    @Autowired
    private PsupFixedPriceService psupFixedPriceService;

    /**
     * 分页查询
     *
     * @return
     */
    @PostMapping(value = "/queryFixedPricePage")
    @ApiOperation(value = "人送一口价分页查询", notes = "人送一口价分页查询", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PsupFixedPriceVO.class),
            @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<PsupFixedPriceVO>> queryFixedPricePage(@RequestBody Page<PsupFixedPrice> page) {
        log.info("PsupFixedPriceController.querySdBaseDataInfoPage param: {}", page);
        RestfulResponse<Page<PsupFixedPriceVO>> resultResponse = getSuccessResponse();
        try {
            resultResponse.setData(psupFixedPriceService.queryFixedPricePage(page));
        } catch (BaseException e) {
            log.error("PsupFixedPriceController.queryFixedPricePage error:{}", e);
            setBaseExceptionResponse(resultResponse, e.getMessage());
        } catch (Exception e) {
            log.error("PsupFixedPriceController.queryFixedPricePage error:{} ", e);
            setErrorResponse(resultResponse);
        }
        return resultResponse;
    }

    /**
     * 批量审核
     *
     * @return
     */
    @PostMapping(value = "/auditFixedPriceByIdList")
    @ApiOperation(value = "人送一口价批量审核", notes = "人送一口价的批量审核", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Object> auditFixedPriceByIdList(@RequestBody CommonVO<Long> commonVO) {
        log.info("PsupFixedPriceController.auditFixedPriceByIdList param: {}", commonVO);
        RestfulResponse<Object> resultResponse = getSuccessResponse();
        try {
            if (Objects.isNull(commonVO) || CollectionUtils.isEmpty(commonVO.getEntityParams())) {
                throw new BaseException("参数为空, 请选择数据!");
            }
            List<String> resultMst = psupFixedPriceService.auditBatchFixedPrice(commonVO.getEntityParams());
            if (CollectionUtils.isNotEmpty(resultMst)) {
                setErrorListResponse(resultResponse, resultMst);
                return resultResponse;
            }
        } catch (BaseException e) {
            log.error("PsupFixedPriceController.auditFixedPriceByIdList error:{}", e);
            setBaseExceptionResponse(resultResponse, e.getMessage());
        } catch (Exception e) {
            log.error("PsupFixedPriceController.auditFixedPriceByIdList error:{} ", e);
            setErrorResponse(resultResponse);
        }
        return resultResponse;
    }

    /**
     * 批量删除
     *
     * @return
     */
    @PostMapping(value = "/deleteBatchFixedPrice")
    @ApiOperation(value = "人送一口价的批量删除", notes = "人送一口价的批量删除", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Object> deleteBatchFixedPrice(@RequestBody CommonVO<Long> commonVO) {
        log.info("PsupFixedPriceController.deleteBatchFixedPrice param: {}", commonVO);
        RestfulResponse<Object> resultResponse = getSuccessResponse();
        try {
            if (Objects.isNull(commonVO) || CollectionUtils.isEmpty(commonVO.getEntityParams())) {
                throw new BaseException("参数为空, 请选择数据!");
            }
            List<String> resultMst = psupFixedPriceService.deleteBatchFixedPrice(commonVO.getEntityParams());
            if (CollectionUtils.isNotEmpty(resultMst)) {
                setErrorListResponse(resultResponse, resultMst);
                return resultResponse;
            }
        } catch (BaseException e) {
            log.error("PsupFixedPriceController.deleteBatchFixedPrice error:{}", e);
            setBaseExceptionResponse(resultResponse, e.getMessage());
        } catch (Exception e) {
            log.error("PsupFixedPriceController.deleteBatchFixedPrice error:{} ", e);
            setErrorResponse(resultResponse);
        }
        return resultResponse;
    }

    /**
     * 根据id获取人送传统价格
     *
     * @return
     */
    @GetMapping(value = "/getSdFixedPriceById/{id}")
    @ApiOperation(value = "根据id获取人送传统价格", notes = "根据id获取人送传统价格", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = RestfulResponse.class),
            @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<PsupFixedPrice> getSdFixedPriceById(@PathVariable("id") Long id) {
        log.info("PsupFixedPriceController.getSdFixedPriceById param: {}", id);
        RestfulResponse<PsupFixedPrice> resultResponse = getSuccessResponse();
        try {
            resultResponse.setData(psupFixedPriceService.selectById(id));
        } catch (BaseException e) {
            log.error("PsupFixedPriceController.getSdFixedPriceById error:{}", e);
            setBaseExceptionResponse(resultResponse, e.getMessage());
        } catch (Exception e) {
            log.error("PsupFixedPriceController.getSdFixedPriceById error:{} ", e);
            setErrorResponse(resultResponse);
        }
        return resultResponse;
    }

    /**
     * 人送基础信息的修改
     *
     * @return
     */
    @PostMapping(value = "/updateSdFixedPrice")
    @ApiOperation(value = "人送传统价格的修改", notes = "人送传统价格的修改", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = RestfulResponse.class),
            @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Object> updateSdFixedPrice(@RequestBody PsupFixedPriceVO psupFixedPriceVO) {
        log.info("PsupFixedPriceController.updateSdFixedPrice param: {}", psupFixedPriceVO);
        RestfulResponse<Object> resultResponse = getSuccessResponse();
        try {
            psupFixedPriceService.updateSdFixedPrice(psupFixedPriceVO);
        } catch (BaseException e) {
            log.error("PsupFixedPriceController.updateSdFixedPrice error:{}", e);
            setBaseExceptionResponse(resultResponse, e.getMessage());
        } catch (Exception e) {
            log.error("PsupFixedPriceController.updateSdFixedPrice error:{} ", e);
            setErrorResponse(resultResponse);
        }
        return resultResponse;
    }
}

